home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / common / sources.c / winutilities.c < prev   
Encoding:
C/C++ Source or Header  |  1996-10-17  |  4.3 KB  |  286 lines

  1. /*  (c) Copyright 1996.  Adobe Systems, Incorporated.  All rights reserved. */
  2.  
  3. #include "WinUtilities.h"
  4.  
  5. #define signatureSize    4
  6. static char cSig[signatureSize] = {'O', 'T', 'O', 'F'};
  7.  
  8. static OSErr memError;
  9.  
  10. void NumToString (const long x, Str255 s)
  11. {
  12.     char c[33] = "";
  13.  
  14.     s [ (s[0]=0)+1 ] = 0;
  15.     ltoa(x, c, 10);
  16.     AppendString(s, c, 0, (short)strlen(c));
  17. }
  18.  
  19. Handle NewHandleClear( long size )
  20. {
  21.     return NewHandle( size );    // GlobalAllocPtr( GHND, ... )
  22.                     // allocates zeroed memory.
  23. }
  24.  
  25. Handle NewHandle ( long size )
  26. {
  27.     Handle mHand;
  28.     char *p;
  29.  
  30.     memError = noErr;
  31.  
  32.     mHand =    (Handle) GlobalAllocPtr (GHND, (sizeof (Handle) + signatureSize));
  33.  
  34.     if (mHand)
  35.         *mHand = (Ptr) GlobalAllocPtr (GHND, size);
  36.  
  37.     if (!mHand || !(*mHand))
  38.         {
  39.         memError = memFullErr;
  40.         return NULL;
  41.         }
  42.  
  43.     // put the signature after the pointer
  44.     p = (char *) mHand;
  45.     p += sizeof (Handle);
  46.     memcpy (p,cSig, signatureSize);
  47.  
  48.     return mHand;
  49.         
  50. }
  51.  
  52. long GetHandleSize(Handle handle)
  53. {
  54.     Ptr p;
  55.     HANDLE h;
  56.  
  57.     memError = noErr;
  58.  
  59.     if (handle)
  60.         {
  61.         p = *handle;
  62.  
  63.         h = GlobalPtrHandle (p);
  64.  
  65.         if (h)
  66.  
  67.             return GlobalSize (h);
  68.         }
  69.  
  70.     memError = nilHandleErr;
  71.  
  72.     return 0L;
  73.  
  74. }
  75.  
  76. void SetHandleSize (Handle handle, long newSize)
  77. {
  78.     Ptr p;
  79.     HANDLE hMem;
  80.  
  81.  
  82.     memError = noErr;
  83.  
  84.     if (handle)
  85.     {
  86.         p = *handle;
  87.  
  88.         if (p)
  89.         {
  90.             GlobalUnlockPtr (p);
  91.             hMem = GlobalReAlloc (GlobalPtrHandle (p), newSize, GHND);    
  92.             if (hMem)
  93.                 p = GlobalLock (hMem);
  94.             else
  95.                 p = NULL;
  96.         }
  97.  
  98.         if (p)
  99.  
  100.             *handle = p;
  101.         else
  102.  
  103.             memError = memFullErr;
  104.     }
  105.  
  106.     else
  107.  
  108.         memError = memWZErr;
  109.  
  110. }
  111.  
  112. void DisposHandle (Handle handle)
  113. {
  114.  
  115.     memError = noErr;
  116.  
  117.     if (handle)
  118.         {
  119.         Ptr p;
  120.  
  121.         p = *handle;
  122.  
  123.         if (p)
  124.             GlobalFreePtr (p);
  125.  
  126.         GlobalFreePtr ((Ptr)handle);
  127.         }
  128.  
  129.     else
  130.  
  131.         memError = nilHandleErr;
  132. }
  133.  
  134. Handle DupHandle(Handle oldhand)
  135. {
  136.     LPSTR        newptr, oldptr;
  137.     Handle    newhand;
  138.     DWORD        size;
  139.  
  140.     oldptr = *oldhand;        // ptr to actual data
  141.  
  142.     size = WSizeBuffer(oldptr);
  143.  
  144.     newhand = NewHandle(size);
  145.     newptr = *newhand;
  146.  
  147. #ifdef WIN32
  148.     memcpy(newptr, oldptr, size);
  149. #else
  150.     hmemcpy(newptr, oldptr, size);
  151. #endif
  152.     return newhand;
  153. }
  154.  
  155. void HLock (Handle h)
  156. {
  157.     GlobalLock(h);
  158. }
  159.  
  160. void HUnlock (Handle h)
  161. {
  162.     GlobalUnlock(h);
  163. }
  164.  
  165. void MoveHHi (Handle h)
  166. {
  167.  
  168. }
  169.  
  170. void DisposPtr(Ptr p)
  171. {
  172.     WFreeBuffer(p);
  173. }
  174.  
  175.  
  176.  
  177. Ptr NewPtr(long size)
  178. {
  179.     return WAllocBuffer(size);
  180. }
  181.  
  182.  
  183.  
  184. Ptr NewPtrClear( long size )
  185. {
  186.     return WAllocBuffer( size );
  187. }
  188.  
  189.  
  190.  
  191. LPSTR WAllocBuffer(long size)
  192. {
  193.     return GlobalAllocPtr(GHND, size);
  194. }
  195.  
  196. VOID WFreeBuffer(LPSTR ptr)
  197. {
  198.     GlobalFreePtr(ptr);
  199. }
  200.  
  201. DWORD WSizeBuffer(LPSTR ptr)
  202. {
  203.     HANDLE    hand;
  204.  
  205.     hand = GlobalPtrHandle(ptr);
  206.     return GlobalSize(hand);
  207. }
  208.  
  209. HANDLE HandFromPtr(LPSTR ptr)
  210. {
  211.     return GlobalPtrHandle(ptr);
  212. }
  213.  
  214. OSErr MemError(void)
  215. {
  216.   return memError;
  217. }
  218.  
  219. /********************* Rect (Photoshop Rect) Functions **************************/
  220. /* WIN32 RECTs are now made of longs so we no longer can use the Windows RECT   */
  221. /* manipulation funcions. The upswing is that there need no longer be a special */
  222. /* Windows case (swap l-t r-b) if fact these won't work if you do...            */
  223. /* Use PISetRect... for Rects and platform specific functions for Windows RECTs */
  224.  
  225. Boolean PISetRect(Rect* pRect, short nLeft, short nTop, short nRight, short nBottom)
  226. {
  227.     if (!pRect)
  228.         return false;
  229.  
  230.     pRect->left = nLeft;
  231.     pRect->top = nTop;
  232.     pRect->right = nRight;
  233.     pRect->bottom = nBottom;
  234.  
  235.     return true;
  236. }
  237.  
  238. Boolean PIOffsetRect(Rect* pRect, short xAmt, short yAmt)
  239. {
  240.     if (!pRect)
  241.         return false;
  242.  
  243.     pRect->left += xAmt;
  244.     pRect->top += yAmt;
  245.     pRect->right += xAmt;
  246.     pRect->bottom += yAmt;
  247.  
  248.     return true;
  249. }
  250.  
  251. Boolean PIPtInRect(Point pt, Rect* r)
  252. {
  253.  
  254.     if (pt.h < r->left || pt.h >= r->right)
  255.         return false;
  256.     if (pt.v < r->top  || pt.v >= r->bottom)
  257.         return false;
  258.     return true;
  259.  
  260. }
  261.  
  262. Boolean PIInsetRect(Rect *pRect, short xAmt, short yAmt)
  263. {
  264.     if (!pRect)
  265.         return false;
  266.  
  267.     pRect->top    += yAmt;
  268.     pRect->bottom -= yAmt;
  269.     pRect->left   += xAmt;
  270.     pRect->right  -= xAmt;
  271.  
  272.     return true;
  273. }
  274.  
  275. long TickCount()
  276. {
  277.     return GetTickCount() / 17;    // S.B. 16.6666666; close enough.
  278. }
  279.  
  280.  
  281. short Random()
  282. {
  283.     return rand();
  284. }
  285.  
  286. /******************************************************************/